package vip.huhaiong.hlfood.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Component
public class JsoupUtil {
    /**
     * 获取指定网址的Document
     * @param url 指定网址
     * @return Document
     */
    public Document getHtmlString(String url) throws Exception{
        Document doc = Jsoup.connect(url).get();
        return  doc;
    }

    /**
     * 获取首页推荐的菜谱列表
     * @param document
     * @return
     */
    public List<Map<String,Object>> getIndexData(Document document){
        List<Map<String,Object>> list = new ArrayList<>();
        Map<String,Object> map = null;
        //用于链接地址跳转
        Elements urls = document.select("div.listtyle1 > a");
        Elements imgs = document.select("div.listtyle1 > a > img");
        for(int i=0; i<urls.size(); i++){
            map = new HashMap<>();
            map.put("id",i);
            map.put("url",urls.get(i).attr("href").toString());
            map.put("title",imgs.get(i).attr("alt").toString());
            map.put("img_url",imgs.get(i).attr("src").toString());
            list.add(map);
        }
        return list;
    }

    public List<Map<String,Object>> getDataDetail(Document document){
        List<Map<String,Object>> list = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        //获取照片
        Element img = new Element("img");
        img.attr("src","https://www.huhailong.vip/img/noimg.png");
        Elements imgElement = document.select("div.recipe_topimgw > img");
        if(imgElement.size()==0){
            img = document.select("div.recipe_topvideow > img").get(0);
        }else{
            img = imgElement.get(0);
        }
//        log.info("图片：{}",img);
        Element gongyi = document.select("div.info2_item1 > strong").get(0);
//        log.info("工艺：{}",gongyi);
        Element kouwei = document.select("div.info2_item2 > strong").get(0);
//        log.info("口味：{}",kouwei);
        Element nandu = document.select("div.info2_item4 > strong").get(0);
//        log.info("难度：{}",nandu);
        Element zbsj = document.select("div.info2_item3 > strong").get(0);
//        log.info("时间：{}",zbsj);
//        Element prsj = document.select("div.info2 > ul.clearfix > li.w270.bb0.br0 > div.processing_w > span.processing").get(0);
        Elements zhuliao = document.select("div.recipe_ingredients > div.right > strong");
//        log.info("主料：{}",zhuliao);
        Elements fuliaos = document.select("div.recipe_ingredients.recipe_ingredients1 > div.right > strong");
//        log.info("辅料：{}",fuliaos);
        Elements bz_title = document.select("div.recipe_step > div.step_content > p");
//        log.info("步骤描述：{}",bz_title);
        Elements bz_img = document.select("div.recipe_step > div.step_content > img");
//        log.info("步骤图片：{}",bz_img);
        Element skills = null;
        try{
            skills =document.select("div.recipe_tips_words").get(0);
//            log.info("技巧：{}",skills.text());
        } catch (Exception e){
            skills = new Element("p");
            skills.html("暂无数据");
        }
        map.put("id",UUID.randomUUID().toString().replace("-","").substring(0,8));
        map.put("img",img.attr("src"));
        map.put("title",img.attr("alt").substring(0,img.attr("alt").length()-3));
        map.put("gy",gongyi.text());
        map.put("kw",kouwei.text());
        map.put("nd",nandu.text());
        map.put("zbsj",zbsj.text());
//        map.put("prsj",prsj.attr("class").replace("processing","").replace("_nd",""));
        map.put("skill", skills.text());
        List<String> zl_list = new ArrayList<>();
        for(Element zl : zhuliao){
            zl_list.add(zl.text());
        }
        map.put("zl",zl_list);
        List<String> fl_list = new ArrayList<>();
        for(Element fl : fuliaos){
            fl_list.add(fl.text());
        }
        map.put("fl",fl_list);
        List<Map<String,Object>> bz_list = new ArrayList<>();
        Map<String,Object> bz_map = null;
        if(bz_img.size()==bz_title.size()) {
            for(int i=0; i<bz_title.size(); i++){
                bz_map = new HashMap<>();
                bz_map.put("id",i);
                bz_map.put("step_name",bz_title.get(i).text());
                bz_map.put("step_img",bz_img.get(i).attr("src"));
                bz_list.add(bz_map);
            }
        }
        map.put("buzhou",bz_list);
        list.add(map);
        return  list;
    }

    public List<Map<String,Object>> getSearchData(String keyworkds) throws Exception{
        List<Map<String,Object>> list = new ArrayList<>();
        Map<String,Object> map = null;
        Document document = getHtmlString("https://so.meishi.cc/?q="+keyworkds);
        Elements food_url = document.select("div.search2015_cpitem > a:has(img)");
        Elements food_img = document.select("div.search2015_cpitem > a:has(img) > img");
        for(int i=0; i<food_url.size(); i++){
            map = new HashMap<>();
            map.put("id",i);
            map.put("name",food_img.get(i).attr("alt"));
            map.put("url",food_url.get(i).attr("href"));
            map.put("img_url",food_img.get(i).attr("src"));
            list.add(map);
        }
        return list;
    }
    public List<Map<String,Object>> getDataByType(String type, Integer page) throws Exception{
        Document document = getHtmlString("https://www.meishij.net/chufang/diy/"+type+"/?&page="+page);
        Elements imgs = document.select("div.listtyle1 > a > img");
        Elements urls = document.select("div.listtyle1 > a");
        List<Map<String,Object>>list = new ArrayList<>();
        Map<String,Object>map = null;
        for(int i=0; i<imgs.size(); i++){
            map = new HashMap<>();
            map.put("id",i);
            map.put("title",imgs.get(i).attr("alt"));
            map.put("img_url",imgs.get(i).attr("src"));
            map.put("url", urls.get(i).attr("href"));
            list.add(map);
        }
        return list;
    }

    public List<Map<String,Object>> getVideoList(Integer page) throws Exception{
        List<Map<String,Object>> list = new ArrayList<>();
        Map<String,Object> map = null;
        Document document = getHtmlString("https://www.meishij.net/video/?page="+page);
        Elements re_url = document.select("div.v_item > a");
        Elements img_url = document.select("div.v_item > a > div.c1 > img");
        Elements title = document.select("div.v_item > a > div.c2 > strong.v_name");
        for(int i=0; i<title.size(); i++){
            map = new HashMap<>();
            map.put("id",i);
            map.put("url",re_url.get(i).attr("href"));
            map.put("img_url",img_url.get(i).attr("src"));
            map.put("title",title.get(i).text());
            list.add(map);
        }
        return list;
    }

    public List<Map<String,Object>> getVideoData(String url) throws Exception{
        List<Map<String,Object>> list = new ArrayList<>();
        Map<String,Object> map = null;
        Document document = getHtmlString(url);
        Element video_src = document.selectFirst("div.vw > video");
        Element info = document.selectFirst("div.v_c3 > p");
        Elements re_url = document.select("div.v_item > a");
        Elements img_url = document.select("div.v_item > a > div.c1 > img");
        Elements title = document.select("div.v_item > a > div.c2 > strong.v_name");
        map = new HashMap<>();
        map.put("src",video_src.attr("src"));
        map.put("info",info.html());
        List<Map<String,Object>> temp = new ArrayList<>();
        for(int i=0; i<title.size(); i++){
            Map<String,Object> about = new HashMap<>();
            about.put("id",i);
            about.put("url",re_url.get(i).attr("href"));
            about.put("img_url",img_url.get(i).attr("src"));
            about.put("title",title.get(i).text());
            temp.add(about);
        }
        map.put("about",temp);
        list.add(map);
        return list;
    }

    public List<Map<String,Object>> getFoodHeatList(Document document){
        Elements elementList = document.select("div.widget-food-list > ul.food-list > li.item");
        List<Map<String,Object>> list = new ArrayList<>();
        for(Element element : elementList){
            Map<String,Object> map = new HashMap<>();
            String detailUrl = element.select("div.img-box > a").attr("href");
            String imgSrc = element.select("div.img-box > a > img").attr("src");
            String title = element.select("div.text-box > h4").text();
            String foodHeat = element.select("div.text-box > p").text();
            map.put("detailUrl", detailUrl);
            map.put("imgSrc", imgSrc);
            map.put("title", title);
            map.put("foodHeat", foodHeat);
            list.add(map);
        }
        return list;
    }

    public List<Map<String,Object>> getFoodHeatDetail(String detailUrl) throws Exception {
        String url = "http://www.boohee.com"+detailUrl;
        Document document = getHtmlString(url);
        List<Map<String,Object>> list = new ArrayList<>();
        Element element = document.select("div.content").get(0);
        Map<String,Object> baseMap = new HashMap<>();
        String foodImg = element.select("div.clearfix > div.food-pic > a > img").attr("src");//详情图片
        baseMap.put("foodImg", foodImg);
        List<Map<String,Object>> baseInfoList = new ArrayList<>();
        Elements baseInfo = element.select("div.clearfix > ul.basic-infor ");
        for(Element base : baseInfo){
            Map<String,Object> map = new HashMap<>();
            Elements tempList = base.select("li");
            for(Element e : tempList) {
                String text = e.select("b").text();
                System.out.println(text);
                String content = e.text();
                if (text.contains("别名")) {
                    String replace = content.replace(text, "");
                    map.put("alias", replace);
                }
                if (text.contains("热量")) {
                    String replace = content.replace(text, "");
                    map.put("heat", replace);
                }
                if (text.contains("分类")) {
                    String replace = content.replace(text, "");
                    map.put("kind", replace);
                }
            }
            baseInfoList.add(map);
        }
        baseMap.put("baseInfo",baseInfoList);
        list.add(baseMap);
        return list;
    }

        public static void main(String[] args) throws Exception{
//            String url = "http://www.boohee.com/food/search?keyword="+"米饭";
//            Document htmlString = new JsoupUtil().getHtmlString(url);
//            System.out.println(htmlString.select("div.widget-food-list > ul.food-list > li.item"));
            List<Map<String, Object>> foodHeatList = new JsoupUtil().getFoodHeatDetail("/shiwu/mifan_zheng");
            System.out.println(JSONObject.toJSONString(foodHeatList));
        }
}
